﻿using System;
using Epitech.Krisk.Xna.UI.Game.Utils;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Epitech.Krisk.Xna.ScreenManagerLib.ScreenManager;
using Epitech.Krisk.Xna.ScreenManagerLib.Widget;

namespace Epitech.Krisk.Xna.UI.Game.Menu
{
    class OptionsView : MenuScreen
    {
        RenderBackground _bg;
        Slider sl;
        Slider sl2;

        public OptionsView()
            : base("Options")
        {

            // Set widgets

            LayoutEntry sll = new LayoutEntry(new Vector2(1100f, 275f));
            sl = new Slider(100, (int)(Music.GetInstance().vol * 100));
            sl.FontSize = 1f;
            sl.SelectedColor = new Color(119, 121, 81);
            sl.DefaultColor = new Color(208, 44, 6);
            sl.IsPulse = false;
            sll.AddEntry(sl);

            LayoutEntry sll2 = new LayoutEntry(new Vector2(1100f, 575f));
            sl2 = new Slider(100, (int)(Sound.GetInstance().vol * 100));
            sl2.FontSize = 1f;
            sl2.SelectedColor = new Color(119, 121, 81);
            sl2.DefaultColor = new Color(208, 44, 6);
            sl2.IsPulse = false;
            sll2.AddEntry(sl2);

            // Create our menu entries.
            LayoutEntry Menu = new LayoutEntry(new Vector2(1575f, 875f));
            Menu.Alignement = LayoutEntry.TextAlignement.Right;
            MenuEntry applyEntry = new MenuEntry("[ Enregistrer ]");
            MenuEntry cancelEntry = new MenuEntry("[ Annuler ]");

            applyEntry.Selected += displayMainMenuApply;
            cancelEntry.Selected += displayMainMenuCancel;

            Menu.AddEntry(applyEntry);
            Menu.AddEntry(cancelEntry);
            // Add entries to the menu.
            MenuEntries.Add(sll);
            MenuEntries.Add(sll2);
            MenuEntries.Add(Menu);

            foreach (MenuEntry item in Menu.MenuEntries)
            {
                item.FontSize = 1f;
                item.SelectedColor = new Color(119, 121, 81);
                item.DefaultColor = new Color(208, 44, 6);
                item.IsPulse = false;
            }
        }

        public override void Draw(GameTime gameTime)
        {
            _bg.Draw(gameTime);
            this.printText("Volume musique", 650f, 300f);
            this.printText("Volume sons", 650f, 600f);
            base.Draw(gameTime);
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            _bg.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }

        public override void LoadContent()
        {
            _bg = new RenderBackground("Game/Background");
            _bg.Content = ScreenManager.Game.Content;
            _bg.SpriteBatch = ScreenManager.SpriteBatch;
            _bg.ViewPort = ScreenManager.GraphicsDevice.Viewport;
            _bg.LoadContent();
            base.LoadContent();
        }

        public void printText(string text, float x, float y)
        {
            SpriteBatch sp = ScreenManager.SpriteBatch;
            SpriteFont font = ScreenManager.Font;

            sp.Begin();

            float transitionOffset = (float)Math.Pow(TransitionPosition, 2);

            // Draw the menu title centered on the screen
            Vector2 titlePosition = new Vector2(x, y);
            Vector2 titleOrigin = font.MeasureString(text) / 2;
            Color titleColor = new Color(255, 255, 255) * TransitionAlpha;
            float titleScale = 0.8f;

            titlePosition.Y -= transitionOffset * 100;

            sp.DrawString(font, text, titlePosition, titleColor, 0,
                                   titleOrigin, titleScale, SpriteEffects.None, 0);

            sp.End();
        }

        #region Handle Input


        /// <summary>
        /// When the user cancels the main menu, ask if they want to exit the sample.
        /// </summary>
        protected override void OnCancel(PlayerIndex playerIndex)
        {
            Sound.GetInstance().setMusic("Music/ButtonSound");
        }

        protected void displayMainMenuApply(object sender, PlayerIndexEventArgs playerIndex)
        {
            // Save selected configurations
            Sound.GetInstance().setMusic("Music/ButtonSound");
            Sound.GetInstance().setSound(sl2.curr);
            Music.GetInstance().setSound(sl.curr);
            ScreenManager.RemoveScreen(this);
            ScreenManager.AddScreen(new MainMenu(), null);
        }

        protected void displayMainMenuCancel(object sender, PlayerIndexEventArgs playerIndex)
        {
            Sound.GetInstance().setMusic("Music/ButtonSound");
            ScreenManager.RemoveScreen(this);
            ScreenManager.AddScreen(new MainMenu(), null);
        }


        #endregion
    }
}
